package com.servlet;

import com.service.UserService;
import com.util.Md5Util;
import com.util.SpringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.NoSuchAlgorithmException;

@WebServlet("/admin/updPassword")
public class UpdPassword extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username =(String) req.getSession().getAttribute("username");
        String oldPassword = req.getParameter("oldPassword");
        String newPassword1 = req.getParameter("newPassword1");
        String newPassword2 = req.getParameter("newPassword2");
        String md5OldPassword=null;
        String md5NewPassword1=null;
        String md5NewPassword2=null;
        try {
            md5OldPassword=Md5Util.toMd5(oldPassword);
            md5NewPassword1=Md5Util.toMd5(newPassword1);
            md5NewPassword2=Md5Util.toMd5(newPassword2);
        } catch (NoSuchAlgorithmException e) {
        }

        UserService userService = SpringUtil.getBean(UserService.class);

        int i = userService.updPassword(username, md5OldPassword, md5NewPassword1, md5NewPassword2);

        PrintWriter writer = resp.getWriter();
        writer.write(i+"");
        writer.close();
    }
}
